<?php
namespace Project_725\Lib;

use TwinPeaks\Core as Core;

/**
 * @return float
 */
function haversine($x)
{
	return sin($x / 2) * sin($x / 2);
}

/**
 * Calculates distance between two points
 *
 * @return float Distance in meters
 */
function calc_distance($lat1, $long1, $lat2, $long2)
{
	$R =  6371009; // Earth's radius in meters

    $lat1_rad = deg2rad($lat1);
    $long1_rad = deg2rad($long1);
    $lat2_rad = deg2rad($lat2);
	$long2_rad = deg2rad($long2);

	$d = 2 * $R * asin(sqrt(haversine($lat2_rad - $lat1_rad) +
		cos($lat2_rad) * cos($lat1_rad) * haversine($long2_rad - $long1_rad)));

	return intval(round($d));
}

/**
 * @return string
 */
function generate_access_token()
{
	return Core\get_unique_string(24);
}
